# coding=utf-8

from rest_framework import serializers
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from io import BytesIO
from movies.models import Movie

class MovieSerializers(serializers.Serializer):
    # 声明模型的字段
    pk = serializers.IntegerField(read_only=True)
    name = serializers.CharField(max_length=30)
    movie_cate = serializers.CharField(max_length=30)
    movie_img = serializers.CharField(max_length=300)
    release_date = serializers.DateField()
    viewed = serializers.BooleanField(default=False)
    created = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
    # 声明模型创建方法
    def create(self, validated_data):
        return Movie.objects.create(**validated_data)

    # 声明模型更新方法
    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.movie_cate = validated_data.get('movie_cate', instance.movie_cate)
        instance.movie_img = validated_data.get('movie_img', instance.movie_img)
        instance.release_date = validated_data.get('release_date', instance.release_date)
        instance.viewed = validated_data.get('viewed', instance.viewed)

        instance.save()
        return instance